#include"globalVar.h"
const wchar_t *items[]={
	L"开始",
	L"远程服务",
	L"我的收藏",
	L"帮助",
	L"设置",
	L"关于",
};
HFONT hFont;
LRESULT CALLBACK MenuProc(HWND hwnd,UINT uint,WPARAM wparam,LPARAM lparam){
	static char traced=0;
switch(uint){
	case WM_CREATE:{
		hFont = CreateFont(
			16,                        // 字体高度
			0,                         // 自动宽度
			0, 0,                      // 无倾斜
			FW_NORMAL,                  // 常规粗细
			FALSE, FALSE, FALSE,        // 非斜体/下划线/删除线
			DEFAULT_CHARSET,
			OUT_DEFAULT_PRECIS,
			CLIP_DEFAULT_PRECIS,
			CLEARTYPE_QUALITY,          // 关键：ClearType 抗锯齿
			DEFAULT_PITCH | FF_DONTCARE,
			L"Microsoft YaHei"          // 推荐使用支持 ClearType 的字体
		);
	}break;
	case WM_DESTROY:{
		DeleteObject(hFont);
	}break;
	case WM_ERASEBKGND:{
		PAINTSTRUCT ps;
		HDC hdc=BeginPaint(hwnd,&ps);
		RECT dirty=ps.rcPaint;
		FillRect(hdc,&dirty,backgroundBrush);
		RECT tmp={dirty.left,dirty.top,dirty.left+borderWidth,dirty.bottom};
		HBRUSH hb=CreateSolidBrush(borderColor);
		FillRect(hdc,&tmp,hb);
		tmp.left=0;
		tmp.top=0;
		tmp.right=dirty.right;
		tmp.bottom=dirty.top+borderWidth;
		FillRect(hdc,&tmp,hb);
		tmp.left=dirty.right-borderWidth;
		tmp.top=dirty.top;
		tmp.right=dirty.right;
		tmp.bottom=dirty.bottom;
		FillRect(hdc,&tmp,hb);
		tmp.left=0;
		tmp.top=dirty.bottom-borderWidth;
		tmp.right=dirty.right;
		tmp.bottom=dirty.bottom;
		FillRect(hdc,&tmp,hb);
		DeleteObject(hb);
		SelectObject(hdc, hFont);
		itemTextX=4;
		SetBkMode(hdc,TRANSPARENT);
		tmp.right=dirty.right;
		for(int index=0;index<itemCount;index++){
			wchar_t *str=items[index];
			tmp.left=itemTextX;
			tmp.top=itemHeight*index;
			tmp.bottom=itemHeight*index+itemHeight;
			DrawTextW(hdc,str,lstrlenW(str),&tmp,DT_VCENTER|DT_SINGLELINE);
		}
		EndPaint(hwnd,&ps);
		return TRUE;
	}break;
	case WM_MOUSELEAVE:{
		traced=0;
		if(itemOnHover>=0){
			RECT rcWin;GetWindowRect(hwnd,&rcWin);
			int ww=rcWin.right-rcWin.left,hh=rcWin.bottom-rcWin.top;
			HDC hdc=GetDC(hwnd);
			IntersectClipRect(hdc,borderWidth,borderWidth,ww-borderWidth,hh-borderWidth);
			int top=itemOnHover*itemHeight;
			SelectObject(hdc,hFont);
			SetBkMode(hdc,TRANSPARENT);
			RECT tmp={0,top,ww,top+itemHeight};
			FillRect(hdc,&tmp,backgroundBrush);
			tmp.left=itemTextX;
			DrawTextW(hdc,items[itemOnHover],lstrlenW(items[itemOnHover]),&tmp,DT_VCENTER|DT_SINGLELINE);
			ReleaseDC(hwnd,hdc);
		}
		itemOnHover=-1;
		TRACKMOUSEEVENT tme={sizeof(tme),TME_CANCEL,hwnd};
		TrackMouseEvent(&tme);
		POINT cursorNow;GetCursorPos(&cursorNow);
		RECT rcgui={0};

		if(!IsWindow(gui)){
			char tmp[100];sprintf(tmp,"Error,invalid window handle!\nvar value==%p",gui);
			MessageBox(NULL,tmp,"",0);
			return 0;
		}
		GetWindowRect(gui,&rcgui);
		//char tmp[100];sprintf(tmp,"%d,%d,%d,%d",rcgui.left,rcgui.top,rcgui.right,rcgui.bottom);MessageBox(NULL,tmp,"",0);
		if(!PtInRect(&rcgui,cursorNow)){
			ShowWindow(hwnd,SW_HIDE);
			//UpdateWindow(hwnd);
			//MessageBox(NULL,"hiding menu","",0);
		}
		ScreenToClient(gui,&cursorNow);
		if(PtInRect(&ivMenu,cursorNow)){
		}else{
			//ShowWindow(hwnd,SW_HIDE);
		}
	}break;
	case WM_MOUSEMOVE:{
		if(!traced){
			TRACKMOUSEEVENT tme={sizeof(tme),TME_LEAVE,hwnd};
			traced=1;
			if (!TrackMouseEvent(&tme)) {
           			 // 跟踪失败处理
           	 		DWORD err = GetLastError();
            			char msg[200];sprintf(msg,"error code==%d",err);MessageBox(NULL,"can not track mouse leave",msg,0);
			}
			TrackMouseEvent(&tme);
		}
		POINT cursorNow;
		GetCursorPos(&cursorNow);
		ScreenToClient(hwnd,&cursorNow);
		int row=cursorNow.y/itemHeight;
		if(row<itemCount&&row>=0){
			if(itemOnHover>=0){
				if(itemOnHover!=row){
					HDC hdc=GetDC(hwnd);
					SelectObject(hdc,hFont);
					SetBkMode(hdc,TRANSPARENT);
					int itemTop=itemOnHover*itemHeight;
					RECT rcWin;GetWindowRect(hwnd,&rcWin);
					int ww=rcWin.right-rcWin.left;
					int hh=rcWin.bottom-rcWin.top;
					IntersectClipRect(hdc,borderWidth,borderWidth,ww-borderWidth,hh-borderWidth);
					RECT tmp={borderWidth,itemTop,ww-borderWidth,itemTop+itemHeight};
					FillRect(hdc,&tmp,CreateSolidBrush(0x00ffffff));

					tmp.left=itemTextX;
					tmp.top=itemTop;
					tmp.right=ww;
					tmp.bottom=itemTop+itemHeight;
					DrawTextW(hdc,items[itemOnHover],lstrlenW(items[itemOnHover]),&tmp,DT_VCENTER|DT_SINGLELINE);
					tmp.left=borderWidth;
					tmp.top=row*itemHeight;
					tmp.right=ww-borderWidth;
					tmp.bottom=tmp.top+itemHeight;
					FillRect(hdc,&tmp,CreateSolidBrush(itemOnHoverColor));
					tmp.left=itemTextX;
					tmp.right=ww;
					DrawTextW(hdc,items[row],lstrlenW(items[row]),&tmp,DT_VCENTER|DT_SINGLELINE);
					ReleaseDC(hwnd,hdc);
					itemOnHover=row;
				}
			}else{
				RECT rcWin;GetWindowRect(hwnd,&rcWin);
				HDC hdc=GetDC(hwnd);
				int ww=rcWin.right-rcWin.left;
				int hh=rcWin.bottom-rcWin.top;
				IntersectClipRect(hdc,borderWidth,borderWidth,ww-borderWidth,hh-borderWidth);
				SelectObject(hdc,hFont);
				SetBkMode(hdc,TRANSPARENT);
				int itemTop=row*itemHeight;
				RECT tmp={borderWidth,itemTop,ww-borderWidth,itemTop+itemHeight};
				FillRect(hdc,&tmp,CreateSolidBrush(itemOnHoverColor));
				tmp.left=itemTextX;
				tmp.top=itemTop;
				tmp.right=ww;
				tmp.bottom=itemTop+itemHeight;
				DrawTextW(hdc,items[row],lstrlenW(items[row]),&tmp,DT_VCENTER|DT_SINGLELINE);
				ReleaseDC(hwnd,hdc);
				itemOnHover=row;
			}
		
		}else if(itemOnHover>=0){
			RECT rcWin;GetWindowRect(hwnd,&rcWin);
			HDC hdc=GetDC(hwnd);
			int ww=rcWin.right-rcWin.left;
			int hh=rcWin.bottom-rcWin.top;
			IntersectClipRect(hdc,borderWidth,borderWidth,ww-borderWidth,hh-borderWidth);
			int itemTop=itemOnHover*itemHeight;
			RECT tmp={borderWidth,itemTop,rcWin.right-borderWidth,itemTop+itemHeight};
			FillRect(hdc,&tmp,CreateSolidBrush(0x00ffffff));
			tmp.left=itemTextX;
			tmp.top=itemTop;
			tmp.right=rcWin.right-rcWin.left;
			tmp.bottom=itemTop+itemHeight;
			DrawTextW(hdc,items[itemOnHover],lstrlenW(items[itemOnHover]),&tmp,DT_VCENTER|DT_SINGLELINE);
			ReleaseDC(hwnd,hdc);
			itemOnHover=-1;
		}
	}break;
	case WM_PAINT:{
		/*PAINTSTRUCT ps;
		HDC hdc=BeginPaint(hwnd,&ps);
		RECT dirty=ps.rcPaint;
		int textX=4;
		for(int index=0;index<itemCount;index++){
			wchar_t *str=items[index];
			TextOutW(hdc,textX,itemHeight*index,str,lstrlenW(str));
		}
		EndPaint(hwnd,&ps);*/
	}break;
}
return DefWindowProc(hwnd,uint,wparam,lparam);
}